﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class CalendarQueryManager_UserDiaryShow : System.Web.UI.Page {
    private DateTime now;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            if(Request.QueryString.Get("s_id") != null && Request.QueryString.Get("dt_st") != null) {
                int s_id = int.Parse(Request.QueryString.Get("s_id"));
                now = DateTime.Parse(Request.QueryString.Get("dt_st")).Date;
                DataList_DataTable(s_id, now);
                if(DataList1.DataSource == null) {
                    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('该部门暂时还无员工！');", true);
                }
                ViewState["now"] = now;
            }
        }
    }

    protected void DataList_DataTable(int s_id, DateTime now) {
        UserManager userManager = new UserManager();
        List<User> list = userManager.findBySectionId(s_id);
        List<UserCalendarArrangeInfo> listCalendars = null;
        if(list != null && list.Count > 0) {
            listCalendars = new List<UserCalendarArrangeInfo>();
            UserCalendarArrangetbService userCalendarService = new UserCalendarArrangetbService();
            foreach(User user in list) {
                UserCalendarArrangeInfo ucaInfo = new UserCalendarArrangeInfo();
                ucaInfo.UserId = user.UserId;
                ucaInfo.UserName = user.UserName;
                ucaInfo.UserLoginName = user.LoginName;
                List<UserCalendarArrangetb> cas = userCalendarService.findCurDateAllByUserLoginNameAndCurDate(user.LoginName, now);
                if(cas != null && cas.Count > 0) {
                    foreach(UserCalendarArrangetb uca in cas) {
                        ucaInfo.UserAllCalendarInfo += (uca.CalendarArrangeStartTime + "-" + uca.CalendarArrangeEndTime + "<br />" +
                            uca.CalendarArrangeContent + "<br />");
                    }
                } else {
                    ucaInfo.UserAllCalendarInfo = "无日程安排！";
                }
                listCalendars.Add(ucaInfo);
            }
        }
        if(listCalendars != null && listCalendars.Count > 0) {
            DataTable dt = new DataTable();
            dt.Columns.Add("UserId", typeof(string));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("UserLoginName", typeof(string));
            dt.Columns.Add("UserAllCalendarInfo", typeof(string));
            foreach(UserCalendarArrangeInfo ucaInfo in listCalendars) {
                DataRow dr = dt.NewRow();
                dr["UserId"] = ucaInfo.UserId.ToString();
                dr["UserName"] = ucaInfo.UserName;
                dr["UserLoginName"] = ucaInfo.UserLoginName;
                dr["UserAllCalendarInfo"] = ucaInfo.UserAllCalendarInfo;
                dt.Rows.Add(dr);
            }
            this.DataList1.DataSource = dt.DefaultView;
        } else {
            this.DataList1.DataSource = null;
        }
        DataList1.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e) {
        if(ViewState["now"] != null) {
            now = (DateTime) ViewState["now"];
        }
        string userLoginName = ((LinkButton) sender).Attributes["u_ln"];
        Response.Redirect("AppendCalendar.aspx?u_ln=" + userLoginName + "&c_dt=" + now.ToShortDateString());
    }
}
